var cpbh="";
$(document).ready(function(){
	//日期函数
	$('.dateInput').datetimepicker({
			format: 'yyyy-mm-dd',
	        forceParse: 0,
	        todayBtn:  1,
	        autoclose: 1,
	        todayHighlight: 1,
	        minView: 2,
	        bootcssVer:3
	});	
	$('.dateInput').datetimepicker('setStartDate', '1900-01-01');
	var endDate = new Date();
	$('.dateInput').datetimepicker('setEndDate', endDate);
	$("#functionNav").attr("href",navAddr);
	//获取并填充提用原因
	var tyyyJson = ajaxResult("get",false,ProjectName+"/sysmanage.getIdAndNameInXtjb.do",{'tableName':'xtjb_tyyy'});
	loadSelect("#form_dtgl select[name=tqyy]",tyyyJson.rows,'id','name');
	//获取查询条件
	var searchParams =$("#form1").serializeObject();	
	//初始化寄存单据Table	
	createTable('#jcdjTable',ProjectName+'/colmanage.getLsDjListByUserid.do','id',tblColTitle,searchParams);	
	//点击查询按钮
	 $("#search_btn").click(function(){
		 refreshTable('#jcdjTable','#form1');
	 });
	//新建寄存单据按钮
	$("#addJcdj_btn").click(function(){
		window.location.href = "jcdjInfo.html";
	})	
	
	//初始化寄存藏品Table	
	var searchJccpParams =$("#form2").serializeObject();	
	$("#jccpList_btn").click(function(){		
		createTable('#jccpTable',ProjectName+'/colmanage.getLsCpListByUserid.do','id',jccpColTitle,searchJccpParams);
	})
	//点击藏品查询按钮
	 $("#schCp_btn").click(function(){
		 refreshTable('#jccpTable','#form2');
	 });
	
	//保存寄存藏品动态管理信息
	$("#dtgl_btn").click(function(){
		if(checkform("#form_dtgl")){
			var params =$("#form_dtgl").serializeObject();
			var savecpJson = ajaxResult("get",false,ProjectName+"/colmanage.addLsDtxx.do",params);
			 if(savecpJson.status=="ok"){
				 $('#dtglTable').bootstrapTable('refresh');
				 $("#form_dtgl")[0].reset();
				 $("#form_dtgl input[name=id]").val("");
				 $("#form_dtgl input[name=opType]").val("add");
				 $(this).text("增加");
				 alertFunc("寄存藏品动态管理信息保存成功！");
			 }else{
				 alertFunc(saveJson.error);
			 }
		}
	});
	
	//结束寄存单据
	$("#btn_add_jsdj").click(function(){
		var kssj = $("#form_jsdj input[name=kssj]").val();
		var jssj = $("#form_jsdj input[name=jssj]").val();
		if(checkEndTime(kssj,jssj)){
			if(checkform("#form_jsdj")){
				var params =$("#form_jsdj").serializeObject();
				var dbResult = ajaxResult("get",false,ProjectName+"/colmanage.finishLsDj.do",params);
				if(dbResult["status"] == 'ok'){
				   $('#jsdjModal').modal('hide');
				   $('#jcdjTable').bootstrapTable('refresh');
			    }else{
				   alert(dbResult["error"]);
			    }		
			}
		}else{
			alertFunc("结束寄存日期必须晚于开始寄存日期！"); 
		}
	});
	
	//结束寄存藏品
	$("#btn_add_jscp").click(function(){
		var kssj = $("#form_jscp input[name=kssj]").val();
		var jssj = $("#form_jscp input[name=jssj]").val();
		if(checkEndTime(kssj,jssj)){
			if(checkform("#form_jscp")){
				var params =$("#form_jscp").serializeObject();
				var dbResult = ajaxResult("get",false,ProjectName+"/colmanage.finishLsCp.do",params);
				if(dbResult["status"] == 'ok'){
					$('#jscpModal').modal('hide');
				   $('#jccpTable').bootstrapTable('refresh');
			    }else{
				   alert(dbResult["error"]);
			    }		
			}
		}else{
			alertFunc("结束寄存日期必须晚于开始寄存日期！"); 
		}
	});
})

//修改藏品动态管理信息
function modiDtgl(data){
	cpbh =data.id;
	//填充动态管理信息
	loadData("#form_dtgl",data);
	$("#form_dtgl input[name=opType]").val("update");
	//填充动态管理中的按钮文字
	$("#form_dtgl #dtgl_btn").text("修改");
}

//删除寄存单据
function delDj(id){
	bootbox.setLocale("zh_CN");
	bootbox.confirm("删除寄存单据后，该单据中的寄存藏品和相关资料也会被删除，确认要删除么？",function(r){
		if(r){
			var params = new Object();
			var deldjJson = ajaxResult("get",false,ProjectName+"/colmanage.deleteLsDj.do",{'id':id});
			if(deldjJson.status=='ok'){				
				$('#jcdjTable').bootstrapTable('refresh');
			}else{
				alertFunc(delyxJson.error);
			}	
		}
	});
}

//结束寄存单据
function finishDj(id,kssj){
	$("#form_jsdj input[name=id]").val(id);
	$("#form_jsdj input[name=kssj]").val(kssj);
	$('#jsdjModal').modal('show');
}



//结束寄存藏品
function finishJccp(id,lsdjbh,kssj){
	$("#form_jscp input[name=ids]").val(id);
	$("#form_jscp input[name=lsdjbh]").val(lsdjbh);
	$("#form_jscp input[name=kssj]").val(kssj);
	$('#jscpModal').modal('show');	
}

//维护动态信息
function modiDtxx(lscpbh){
	cpbh = lscpbh;
	$("#form_dtgl input[name=cpbh]").val(cpbh);
	//加载动态管理列表Table	
	createTable('#dtglTable',ProjectName+'/colmanage.getLsDtxxList.do','id',dtglColTitle,{'cpbh':cpbh});	
	$('#jccpModal').modal('show');
}

var tblColTitle = [{
	        field: 'djbh',
	        title: '单据编号'
	    }, {
	        field: 'bt',
	        title: '标题',
	        formatter: function(value, row, index) {	        	
	        	return '<a href="jcdjDetail.html?lsdjbh='+row.id+'&page=jcList.html">'+value+'</a>';
		      }
	    }, {
	        field: 'jcdw',
	        title: '寄存单位'
	    }, {
	        field: 'kssj',
	        title: '开始寄存时间'
	    }, {
	        field: 'jssj',
	        title: '结束时间'
	    }, {
	        field: 'bcbmmc',
	        title: '保存部门名称'
	    },{
	        field: 'bcrmc',
	        title: '保存人名称'
	    },{
	        field: 'xtglymc',
	        title: '协同管理员名称'
	    },{
	        field: '',
	        title: '操作',
	        formatter: function(value, row, index) {
	        	var result="";	 
	        	if(row.jssj==null || row.jssj==''){
		    		if(getUserRight()){
		    			if(row.xtglyid.indexOf(userInfo.userid) > -1 )	   
		    				var kssj='\''+dataFormat(row.kssj)+'\'';
		        			result = '<a href="jcdjInfo.html?lsdjbh='+row.id+'">维护</a>&nbsp;&nbsp;&nbsp;&nbsp;<a onclick="finishDj('+row.id+','+kssj+')" href="javascript:void(0)">结束寄存</a>&nbsp;&nbsp;&nbsp;&nbsp;<a onclick="delDj('+row.id+')" href="javascript:void(0)">删除</a>';
	 	    		}
	        	}
	        	return result;
		      }
	    }];

var jccpColTitle = [{
	 checkbox:true,
	 formatter: function(value, row, index) {
   	if(row.jssj!=null && row.jssj!=''){   
   		return {
               disabled: true
           };
   	}
   	return value;
     }
   },{
       field: 'djbh',
       title: '单据编号'
   }, {
       field: 'cpmc',
       title: '藏品名称',
       formatter: function(value, row, index) {	        	
       	return "<a href='jccpDetail.html?cpbh="+row.id+"&page=jcList.html'>"+value+"</a>";
	      }
   }, {
       field: 'cpsl',
       title: '藏品数量'
   }, {
       field: 'cfwz',
       title: '存放位置'
   }, {
       field: 'kssj',
       title: '开始寄存时间'
   }, {
       field: 'jssj',
       title: '归还时间'
   },{
       field: '',
       title: '操作',
       formatter: function(value, row, index) { 
       	var result="";	 
       	if(row.jssj==null || row.jssj==''){   
       		var kssj='\''+dataFormat(row.kssj)+'\'';
   			result = '<a href="jccpInfo.html?cpbh='+row.id+'">维护</a>&nbsp;&nbsp;&nbsp;<a href="jccpDtgl.html?cpbh='+row.id+'">动态</a>&nbsp;&nbsp;&nbsp;<a onclick="finishJccp('+row.id+','+row.lsdjbh+','+kssj+')" href="javascript:void(0)">结束寄存</a>';
       	}
       	return result;
   	}
   }];

var dtglColTitle = [{
    field: 'tqyymc',
    title: '提取原因'
}, {
    field: 'tqsj',
    title: '提取时间'
},{
    field: 'thsj',
    title: '退回时间'
}, {
    field: 'tyr',
    title: '提用人'
}, {
    field: 'jsr',
    title: '经手人'
},{
    field: '',
    title: '操作',
    formatter: function(value, row, index) { 
			return "<a onclick='modiDtgl("+JSON.stringify(row)+")' href='javascript:void(0)'>修改</a>&nbsp;&nbsp;&nbsp;<a onclick='delRecord(\"#dtglTable\",\"/colmanage.deleteLsDtxx.do\",{\"id\":"+row.id+"})' href='javascript:void(0)'>删除</a>";
	}
}];
